--[[ VGUI - BuildMenu Init ]]--
local sx = ScrW()
local sy = ScrH()
BuildMenu = vgui.Create("bBase")
BuildMenu:SetSize(sx/3,sy/4)
BuildMenu:SetFont("Default")
BuildMenu:SetPos(sx-(sx/3)-1,sy-(sy/4)-1)
BuildMenu:SetVisible(false)
--[[ END VGUI ]]--



--[[ 
ToggleBuildMode
	- changes to isometric view
	- enables build menu
]]--
function ToggleBuildMode(um)
	local bool = um:ReadBool()
	LocalPlayer().BuildMode = bool
	gui.EnableScreenClicker(bool)
	BuildMenu:SetVisible(bool)
end
usermessage.Hook("ToggleBuildMode", ToggleBuildMode)



--[[ 
UpdateGhostPos
	- moves the ghosted entity to the mouse coordinates
]]--
function UpdateGhostPos(ghost)
	local trace = MouseToWorld()
	ghost:SetPos(trace.HitPos)
end



--[[ 
StartBuilding
	- Creates a ghosted entity after a player choses an object to build
	- Calls UpdateGhostPos to sync coordinates
]]--
function StartBuilding(turret)
	if LocalPlayer().IsBuilding == true then
		StopBuilding()
	end
	
	LocalPlayer().IsBuilding = true
	local ghost = ents.Create("prop_physics")
	ghost:SetModel(turret.Model)
	ghost:SetPos(LocalPlayer():GetPos())
	ghost:SetOwner(LocalPlayer())
	ghost:SetMaterial("models/debug/debugwhite")
	ghost:SetColor(0,255,0,200)
	ghost:Spawn()
	LocalPlayer().Ghost = ghost
	LocalPlayer().BuildTurret = turret
	timer.Create("UpdateGhostPos"..ghost:EntIndex(), 0.01, 0, UpdateGhostPos, ghost)
end

--[[ 
StopBuilding
	- Removes the ghosted entity
	- Breaks the UpdateGhostPos timer
]]--
function StopBuilding()
	timer.Destroy("UpdateGhostPos"..LocalPlayer().Ghost:EntIndex())
	LocalPlayer().IsBuilding = false
	LocalPlayer().Ghost:Remove()
	LocalPlayer().Ghost = nil
	LocalPlayer().BuildTurret = nil
end


--[[ 
MouseToWorld
	- Converts mouse coordinates (local) to map coordinates (world)
	- Does a trace and returns the result
]]--
function MouseToWorld()
	local dir = LocalPlayer():GetCursorAimVector();  
	local traceRes = util.QuickTrace(myVec.origin, dir*1e9, LocalPlayer()) 
	return traceRes
end



--[[ 
clickedOnScreen
	- Checks if user clicked on the screen
	- Used to build turrets and show their information
]]--
function clickedOnScreen( click, mousecode, vec ) 

	if click and mousecode == MOUSE_LEFT then
	
		if LocalPlayer().BuildMode then
	
			local traceRes = MouseToWorld()
			
			-- if not in turret-build mode
			if not LocalPlayer().IsBuilding	and not LocalPlayer().Ghost then
				if traceRes.HitNonWorld and traceRes.Entity:IsValid() then
					local e = traceRes.Entity
					if e.TurretNR then
						RunConsoleCommand("showTurretInfo", e:EntIndex())
					end
				end
				return
			else	-- player has a ghost-turret
				if traceRes.HitNonWorld then
					LocalPlayer():ChatPrint("Cannot build here")
					return
				else
					local h = traceRes.HitPos
					local a = LocalPlayer().Ghost:GetAngles()
					RunConsoleCommand("buildTurret", LocalPlayer().BuildTurret.ENT, h.x, h.y, h.z, a.p, a.y, a.r)
					StopBuilding()
				end
			end
			
		end
		
	elseif click and mousecode == MOUSE_RIGHT then
			
		if LocalPlayer().BuildMode then	
		
			if LocalPlayer().IsBuilding	and LocalPlayer().Ghost then
				StopBuilding()			
			end
			
		end
	end
	
end
hook.Add( "CallScreenClickHook", "ClickedOnTheScreen", clickedOnScreen )




function MyCalcView(ply, pos, angles, fov)
	if LocalPlayer().BuildMode == true then
		local view = {}
		view.origin = pos-(angles:Forward()*400)
		view.angles = angles
		view.fov = fov
		
		myVec = view
		
		return view
	end
end
hook.Add("CalcView", "MyCalcView", MyCalcView) 
hook.Add("ShouldDrawLocalPlayer", "MyHax ShouldDrawLocalPlayer", function(ply)
        return LocalPlayer().BuildMode
end) 
 
 
function CheckPlayerBinds(pl, bind, pressed)
	if string.find( bind, "+reload" ) then
		if pl.IsBuilding and pl.Ghost then
			local a = pl.Ghost:GetAngles()
			pl.Ghost:SetAngles(Angle(a.p,a.y+12.5,a.r))
		end
	end		
end
hook.Add("PlayerBindPress", "myBindHook", CheckPlayerBinds)